/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.table.TableColumnModel;

import org.apache.log4j.Logger;

import who.tmap.bo.cd.CDColorPattern;
import who.tmap.bo.cd.ColorPatternClassification;
import who.tmap.common.TMapException;
import who.tmap.toolkit.impl.AnnotationsHelper;
import who.tmap.ui.util.ColumnWidthsHandler;

/**
 *  
 * This class is the event listener added to the jComboBoxAttrClassification
 * When selected item changes, the RangeInfo tab is updated depending on the
 * classification in use:<BR>
 *<UL>
 *	<LI>Non-Numeric Classification: hide from and to columns, range number and missing data range, and populate new ranges
 *  <LI>Other: hide the caption column and display range number and missing data range
 *</UL>
 *Finally, modifying legend if necessary
 */
public class ClassificationAction implements ActionListener {
	
    private static Logger logger = Logger.getLogger(ClassificationAction.class);
	
	private ColumnWidthsHandler colHandler;
	private Integer prevSelectedIndex;
	private Integer prevRangeNumber;
	
	private JPanelCDColorPattern jPanel;
	private JPanelCDColorPatternHelper helper;
	
	public ClassificationAction( JPanelCDColorPattern jPanel ) {
		this.jPanel =jPanel;
		helper =new JPanelCDColorPatternHelper( jPanel );
		
		prevSelectedIndex =jPanel.getJComboBoxAttrClassification().getSelectedIndex();
		prevRangeNumber =JPanelCDColorPattern.DEFAULT_NB_RANGES;
		
		colHandler =new ColumnWidthsHandler( jPanel.getJTableRange() );
	}
	
	public void actionPerformed(ActionEvent e) {
		Integer newSelectedIndex =jPanel.getJComboBoxAttrClassification().getSelectedIndex();
		Integer newRangeNumber =null;
		
		jPanel.dataModel.setClassification( jPanel.getJComboBoxAttrClassification().getSelectedIndex() );
		
		// Check if the user defined ranges has changed since it's necessary to create a new legend
		try {
			newRangeNumber = Integer.parseInt( jPanel.getJTextFieldNbRanges().getText() );
		} catch(NumberFormatException numberFormatException) {
			newRangeNumber = JPanelCDColorPattern.DEFAULT_NB_RANGES;
		}
		
		if ( newSelectedIndex == ColorPatternClassification.NON_NUMERIC ) {
			try {
				
				JPanel rangePanel =jPanel.getJPanelRangeInfo();
				TableColumnModel rangeColumnModel =jPanel.getJTableRange().getColumnModel();
				
				helper.processNonNumeric();
				
				jPanel.getJLabelNbRanges().setVisible( false );
				jPanel.getJTextFieldNbRanges().setVisible( false );
				
				colHandler.setVisible( false, RangeTableModel.FROM_COL );
				colHandler.setVisible( false, RangeTableModel.TO_COL );
				colHandler.setVisible( true, RangeTableModel.CAPTION_COL );
									
				rangePanel.revalidate();				
			} catch (TMapException e1) {
				e1.printStackTrace();
			}
		} else { 
			
			JPanel rangePanel =jPanel.getJPanelRangeInfo();
			CDColorPattern cdc =(CDColorPattern)jPanel.getThematicMap().getCartographicDisplay();
			
			jPanel.getJLabelNbRanges().setVisible( true );
			jPanel.getJTextFieldNbRanges().setVisible( true );
			
			colHandler.setVisible( true, RangeTableModel.FROM_COL );
			colHandler.setVisible( true, RangeTableModel.TO_COL );
			colHandler.setVisible( false, RangeTableModel.CAPTION_COL );
			
			jPanel.updateRanges();
			
			rangePanel.revalidate();
		}
		
		Boolean updatingLegend =  ( prevSelectedIndex != ColorPatternClassification.NON_NUMERIC && 
							        newSelectedIndex == ColorPatternClassification.NON_NUMERIC )
							    || ( prevSelectedIndex == ColorPatternClassification.NON_NUMERIC && newSelectedIndex == ColorPatternClassification.NON_NUMERIC )
							    || ( prevSelectedIndex == ColorPatternClassification.NON_NUMERIC && newSelectedIndex != ColorPatternClassification.NON_NUMERIC );    
		
		if( updatingLegend || ( prevRangeNumber != newRangeNumber && prevRangeNumber != 0 ) ) {
			new AnnotationsHelper().updateLegend( jPanel.getThematicMap().getAnnotations(), newSelectedIndex );
		}
		
		prevSelectedIndex = newSelectedIndex;
		prevRangeNumber = newRangeNumber;
		
		jPanel.setDirty( true );
	}
	
}
